﻿
function previewprocess(p) {
    $('.pre-applyer').peopleFinder();
    $('.pre-proxyapplyer').peopleFinder();
    //$('.radio').buttonset();
    initialForm(p.fields);
    intialApproves();

    $('#pre-agree,#pre-start').click(function () {
        var pi = getProcessInstance();
        $.ajax({
            url: "/ProcessRoll/GetNextParticipant",
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: pi,
            success: function (pi) {
                var $approvelist = $('.approveul');
                $approvelist.children().remove();
                for (var i = 0; i < pi.ActivityInstances.length; i++) {
                    var cai = pi.ActivityInstances[i];
                    var ca = cai.Activity;
                    if (ca) {
                        var $li;
                        if (cai.ExameApprove)
                            $li = $('<li><div><span>参与人：' + cai.ExameApprove.Approver + '</span><span>审批结果：' + cai.ExameApprove.ResultEA + '</span><lable> 说明：' + cai.ExameApprove.Comments + '</lable></div></li>').appendTo($approvelist);
                        else
                            $li = $('<li><textarea class="commentarea"></textarea></li>').appendTo($approvelist);

                        $li.data('lidata', cai);
                    }
                }
                if ($approvelist.children().last().find('.commentarea').length == 0) {
                    $('#appbutons').hide();
                }
            }
        });
    });
}

function intialApproves(){
    $('.pre-approve, #pre-start').click(function () {
        $('#pre-start').hide();
        $('#span-approve').show();

        if($(this).attr('id')=='pre-agree'){
           approveresult = 'agree';
        }
        else if($(this).attr('id')=='pre-disagree'){
           approveresult = 'disagree';
        }
        else if($(this).attr('id')=='pre-continute'){
           approveresult = 'continute';
       }
        else
            approveresult = 'start';
   });

   $('#pre-start').show();
   $('#span-approve').hide();
}

function getProcessInstance() {
    var participant = $('#participant').val();
    var am = getApplyMsg();
    var ea = getExameApprove();
    var ais = new Array();
    var $approveliitems = $('.approveul').children().each(function () {
        var lidata = $(this).data('lidata');
        if (!lidata.ExameApprove) {
            lidata.ExameApprove = getExameApprove();
            lidata.ExameApprove.Approver = lidata.Activity.ActivityRole;
        }
        ais.push(lidata);
    });
    var pi = { ActivityInstances: ais, ProcessID: processid, ApplyMsg: am};
    return flow = JSON.stringify(pi);
}

function getApplyMsg() {
    var applyer = $('.pre-applyer').val();
    var proxyapplyer = $('.pre-proxyapplyer').val();
    var department = $('.pre-department').val();
    var iv = new Array();
    $('.pre-form').children().each(function () {
        var item = $(this).find('.fieldvalue');
        var v = item.val();
        var n = item.attr('fieldname');
        iv.push({ FieldName: n, FieldValue: v, ID: null });
    });

    return { Applyer: applyer, ProxyApplyer: proxyapplyer, Department: department,FieldInstances: iv };
}

function getExameApprove() {
    var iv = new Array();
    $('.pre-form').children().each(function () {
        var item = $(this).find('.fieldvalue');
        var v = item.val();
        var n = item.attr('fieldname');
        iv.push({ FieldName: n, FieldValue: v, ID: null });
    });
    var ea = { FieldInstances: iv, ResultEA: approveresult, Comments: $('.commentarea').val(),ID: null };
      return ea;
}

function initialForm(fields) {
    var $formul = $('.pre-form');
    for(var i = 0; i < fields.length; i++) {
        var $li = $formul.addListItem(null);
        var width = parseInt(fields[i].FieldWidth) * 25 + '%';
        $li.css('width', width);
        var controltype =  fields[i].ControlType;
        var fieldname = fields[i].FieldName + ":";
        var ovs = fields[i].OptionValue == null ? null : fields[i].OptionValue.split('||');
        var min = fields[i].Min;
        var max = fields[i].Mix;
        $('<label class="dyn-fieldname"/>' + fieldname + '</label>').appendTo($li);

        if (controltype == 'text') {
            $('<input class="dyn-text fieldvalue" type="text"/>').appendTo($li).attr('fieldname', fields[i].FieldName);
        }
        if (controltype == 'number') {
            var $v = $('<input class="dyn-text fieldvalue" type="text"/>').appendTo($li).attr('fieldname', fields[i].FieldName);

            if (min && max && max > min)
                $v.jStepper({ minValue: min, maxValue: max });
            else
                $v.jStepper();
        }
        else if (controltype == 'singleselection') {
//            var $c = $('<div class="dyn-container radio"></div>').appendTo($li);
//                        for (var j = 0; j < ovs.length; j++) {
//                            $('<input type="radio" id="radio' + j + '" name="radio" />').appendTo($c);
//                            $('<label for="radio' + j + '" />' + ovs[j] + '</lable>').appendTo($c);
//                        }
            var $c = $('<span class="dyn-container radioset fieldvalue"></span>').appendTo($li).attr('fieldname', fields[i].FieldName);
            for (var j = 0; j < ovs.length; j++) {
                $('<input type="radio">' + ovs[j] + '</input>').val(ovs[j]).appendTo($c).click(function () {
                    var $this = $(this);
                    var $parent = $this.parent();
                    $parent.find('input').each(function () {
                        $(this).removeAttr("checked");
                    });
                    $this.attr('checked', 'checked');
                    $parent.val($this.val());
                });
            }
        }
        else if (controltype == 'dropdownlist') {
            var $c = $('<span class="dyn-container"></span>').appendTo($li);
            var $select = $('<select style="width:60%"  class="select fieldvalue"></select>').appendTo($c).attr('fieldname', fields[i].FieldName);
            $('<option selected="selected" value="-1">--请选择--</option>').appendTo($select);
            for (var j = 0; j < ovs.length; j++) {
                $('<option ></option>').text(ovs[j]).val(ovs[j]).appendTo($select);
            }
            $select.change(function () {
                var selectedoption = $(this).find(':selected').val();
                $select.val(selectedoption);
            });
//            .find(':selected').each(function () {
//                    if ($(this).val() != '-1')
//                        av[i].fieldvalue = $(this).val();
//                });
        }
        else if (controltype == 'multipleselection') {
            var $c = $('<span class="dyn-container fieldvalue"></span>').appendTo($li).attr('fieldname', fields[i].FieldName);
            for (var j = 0; j < ovs.length; j++) {
                $('<input type="checkbox" value="' + ovs[j] + '" class="checkbox">' + ovs[j] + '</input>').appendTo($c).click(function () {
                    var $ck = $(this);
                    if ($ck.attr('isChecked') == 'checked') {
                        $ck.removeAttr('isChecked');
                    }
                    else {
                        $ck.attr('isChecked', 'checked');
                    }
                    var value = '||';
                    $(this).parent().find('input').each(function () {
                        if ($(this).attr('isChecked') == 'checked') {
                            var v = $(this).val();
                            value = value + v + '||';
                        }
                    });
                    $(this).parent().val(value);
                });
            }
        }
        else if (controltype == 'peoplefinder') {
            $('<input type="text"  class="dyn-text fieldvalue"/>').peopleFinder().appendTo($li).attr('fieldname', fields[i].FieldName);
        }
        else if (controltype == 'datepicker') {
            $('<input type="text" class="dyn-text fieldvalue"/>').datepicker().appendTo($li).attr('fieldname', fields[i].FieldName);
        }
        else if (controltype == 'richtext') {
            $('<div style=" minHeight:220"><textarea  style="width:95%; minHeight:220" class="fieldvalue"></textarea></div>').appendTo($li).find('textarea').attr('fieldname', fields[i].FieldName);
            $li.css('minHeight','235px').find('textarea').wysiwyg({
                    controls: {
                        strikeThrough: { visible: true },
                        underline: { visible: true },

                        separator00: { visible: true },

                        justifyLeft: { visible: true },
                        justifyCenter: { visible: true },
                        justifyRight: { visible: true },
                        justifyFull: { visible: true },

                        separator01: { visible: true },

                        indent: { visible: true },
                        outdent: { visible: true },

                        separator02: { visible: true },

                        subscript: { visible: true },
                        superscript: { visible: true },

                        separator03: { visible: true },

                        undo: { visible: true },
                        redo: { visible: true },

                        separator04: { visible: true },

                        insertOrderedList: { visible: true },
                        insertUnorderedList: { visible: true },
                        insertHorizontalRule: { visible: true },

                        h4mozilla: { visible: true && $.browser.mozilla, className: 'h4', command: 'heading', arguments: ['h4'], tags: ['h4'], tooltip: "Header 4" },
                        h5mozilla: { visible: true && $.browser.mozilla, className: 'h5', command: 'heading', arguments: ['h5'], tags: ['h5'], tooltip: "Header 5" },
                        h6mozilla: { visible: true && $.browser.mozilla, className: 'h6', command: 'heading', arguments: ['h6'], tags: ['h6'], tooltip: "Header 6" },

                        h4: { visible: true && !($.browser.mozilla), className: 'h4', command: 'formatBlock', arguments: ['<H4>'], tags: ['h4'], tooltip: "Header 4" },
                        h5: { visible: true && !($.browser.mozilla), className: 'h5', command: 'formatBlock', arguments: ['<H5>'], tags: ['h5'], tooltip: "Header 5" },
                        h6: { visible: true && !($.browser.mozilla), className: 'h6', command: 'formatBlock', arguments: ['<H6>'], tags: ['h6'], tooltip: "Header 6" },

                        separator07: { visible: true },

                        cut: { visible: true },
                        copy: { visible: true },
                        paste: { visible: true }
                    }
                });

                $li.find('#undefinedIFrame').css('minHeight', '160px');
        }
    }
        
}